package com.jn.mvvm2.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jn.mvvm2.entity.HanZi;
import com.jn.mvvm2.repository.HanZiRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class HanZiViewModel extends ViewModel {
    private HanZiRepository mHanZiRepository;
    private MutableLiveData<HanZi> mHanZiLiveData = new MutableLiveData<>();

    public HanZiViewModel() {
        mHanZiRepository = new HanZiRepository();
    }

    public void getData(String word) {
        mHanZiRepository.getData(word).subscribe(new Observer<HanZi>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d("asdf", "onSubscribe");
            }

            @Override
            public void onNext(@NonNull HanZi hanZi) {
                Log.d("asdf", "onNext: " + hanZi.toString());
                mHanZiLiveData.postValue(hanZi);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d("asdf", "onError");
            }

            @Override
            public void onComplete() {
                Log.d("asdf", "onComplete");
            }
        });
    }

    public LiveData<HanZi> getLiveData() {
        return mHanZiLiveData;
    }
}